package middle;

public class Solution_55 {

    /*public boolean canJump(int[] nums) {
        boolean[] array = new boolean[nums.length];
        array[0] = true;
        for (int i = 0; i < nums.length; i++) {
            for (int j = 1; j <= nums[i]; j++) {
                if (i + j < nums.length) {
                    array[i + j] = true;
                }
            }
        }
        return array[array.length - 1];
    }*/

    public boolean canJump(int[] nums) {
        int pre = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (pre == 0) {
                return false;
            }
            pre = Math.max(pre - 1, nums[i]);

        }
        return true;
    }

    public static void main(String[] args) {
        Solution_55 model = new Solution_55();
        System.out.println(model.canJump(new int[]{2, 3, 1, 1, 4}));
        System.out.println(model.canJump(new int[]{3, 2, 1, 0, 4}));
        System.out.println(model.canJump(new int[]{0, 1}));
        System.out.println(model.canJump(new int[]{1, 0}));
    }
}
